package com.example.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.example.entity.RestBean;
import com.example.entity.dto.Interact;
import com.example.entity.dto.TopicType;
import com.example.entity.vo.request.AddCommentVO;
import com.example.entity.vo.request.TopicCreateVO;
import com.example.entity.vo.request.TopicUpdateVO;
import com.example.entity.vo.response.*;
import com.example.mdc.MdcDot;
import com.example.service.TopicService;
import com.example.service.WeatherService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/forum")
public class ForumController {

    @Resource
    WeatherService weatherService;
    @Resource
    TopicService topicService;

    @GetMapping("/weather")
    public RestBean<WeatherVO> weather(double longitude, double latitude) {
        WeatherVO vo = weatherService.fetchWeather(longitude, latitude);
        if (vo == null) {
            return RestBean.failure(400, "获取天气信息失败");
        }
        return RestBean.success(vo);
    }

    @GetMapping("/types")
    public RestBean<List<TopicTypeVO>> listTypes() {
        List<TopicType> topicTypes = topicService.listTypes();
        //将TopicType转换为TopicTypeVO
        List<TopicTypeVO> topicTypeVOS = new ArrayList<>();
        for (TopicType topicType : topicTypes) {
            TopicTypeVO topicTypeVO = new TopicTypeVO(topicType.getId(), topicType.getName(), topicType.getDesc(), topicType.getColor());
            topicTypeVOS.add(topicTypeVO);
        }
        return RestBean.success(topicTypeVOS);
    }

    @PostMapping("/create-topic")
    @MdcDot(bizCode = "#createTopic")
    public RestBean<Void> createTopic(@Valid @RequestBody TopicCreateVO vo, @RequestAttribute("id") int id) {
        String message = topicService.createTopic(id, vo);
        if (message == null) {
            return RestBean.success();
        }
        return RestBean.failure(400, message);
    }

    @GetMapping("/list-topic")
    public RestBean<List<TopicPreviewVO>> listTopic(@RequestParam @Min(0) int page, @RequestParam @Min(0) int type) {
        return RestBean.success(topicService.listTopicByPage(page + 1, type));
    }

    @GetMapping("/top-topic")
    public RestBean<List<TopicTopVO>> topTopic() {
        return RestBean.success(topicService.listTopTopics());
    }

    @GetMapping("topic")
    @MdcDot(bizCode = "#topic")
    public RestBean<TopicDetailVO> topic(@RequestParam @Min(0) int tid, @RequestAttribute("id") int id) {
        return RestBean.success(topicService.getTopic(tid, id));
    }

    @GetMapping("/interact")
    @MdcDot(bizCode = "#interact")
    public RestBean<Void> interact(@RequestParam @Min(0) int tid, @RequestParam @Pattern(regexp = "(like|collect)") String type,
                                   @RequestParam boolean state, @RequestAttribute("id") int id) {
        topicService.interact(new Interact(tid, id, new Date(), type), state);
        return RestBean.success();
    }

    //收藏列表接口
    @GetMapping("/collects")
    public RestBean<List<TopicPreviewVO>> collects(@RequestAttribute("id") int id) {
        return RestBean.success(topicService.listTopicCollects(id));
    }

    @PostMapping("/update-topic")
    public RestBean<String> updateTopic(@Valid @RequestBody TopicUpdateVO vo, @RequestAttribute("id") int id) {
        return RestBean.success(topicService.updateTopic(id, vo));
    }

    //评论接口
    @PostMapping("/add-comment")
    public RestBean<Void> addComment(@Valid @RequestBody AddCommentVO vo, @RequestAttribute("id") int id) {
        if (topicService.createComment(vo, id) == null) {
            return RestBean.success();
        } else {
            return RestBean.failure(400, "评论失败");
        }

    }

    @GetMapping("/comments")
    public RestBean<List<CommentVO>> comments(@RequestParam @Min(0) int tid, @RequestParam @Min(0) int page) {
        return RestBean.success(topicService.comments(tid, page + 1));
    }

    @GetMapping("/delete-comment")
    public RestBean<Void> deleteComment(@RequestParam @Min(0) int id, @RequestAttribute("id") int uid) {
        topicService.deleteComment(id, uid);
        return RestBean.success();
    }
}
